<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <style>
        .content{
            width: 1400px;
            height: 760px;
            border-color: white;
            top: 50%;
            left: 50%;
            margin: 100px;
        }
    </style>
</head>
<body>
<script src="/static/js/jquery-1.8.0.min.js"></script>
<script src="/static/layui/layui.all.js"></script>
<div class="content">
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="title|required" autocomplete="off" placeholder="请输入姓名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="email|required" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="tel" lay-verify="tel|required" autocomplete="off" placeholder="请输入手机号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">登录名</label>
        <div class="layui-input-inline">
            <input type="text" id="loginname" name="loginName" lay-verify="required" autocomplete="off" placeholder="请输入登录名" class="layui-input">
        </div>
        <a class="layui-btn" onclick="checkname()">检测是否已存在</a>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="pass" name="passWord" lay-verify="pass|required" autocomplete="off" placeholder="请输入密码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</div>

<script>
    var cansubmit = "1";
    layui.use('form', function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        //……
        //但是，如果你的HTML是动态生成的，自动渲染就会失效
        //因此你需要在相应的地方，执行下述方法来进行渲染
        form.render();


        form.on('submit(demo1)', function(data){
            $.ajax({
                url:"/register/register.save",
                data:data.field,
                success:function (datas) {
                    if (datas.code=='0'){
                        layer.msg(datas.msg);
                    }else{
                        layer.msg(datas.msg);
                        window.location.href="/index";
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
    function checkname() {
        $.ajax({
            url:"/register/checkname.json",
            data:{
                loginname:$("#loginname").val()
            },
            success:function (datas) {
                if (datas.code=='0'){
                    cansubmit=0;
                    layer.msg(datas.msg);
                }else{
                    layer.msg(datas.msg);
                }
            }
        });
    }
</script>
</body>
</html>